package com.ktjy.ch02.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.UrlBasedViewResolver;


public class BaseController {
    public String forward(String url){
        return UrlBasedViewResolver.FORWARD_URL_PREFIX+url;
    }

    public String redirect(String url){
        return UrlBasedViewResolver.REDIRECT_URL_PREFIX+url;
    }

    public HttpServletRequest request(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public void request(String name,Object obj){
        request().setAttribute(name,obj);
    }
    public <T> T request(String name){
        return (T)request().getAttribute(name);
    }
    public HttpSession session(){
        return request().getSession();
    }

    public void session(String name,Object obj){
        session().setAttribute(name,obj);
    }
    public <T> T session(String name){
        return (T)session().getAttribute(name);
    }

}
